package com.mz.mattress.api.mattress;

import com.geer2.sop.sdk.client.OpenClient;
import com.geer2.sop.sdk.request.CommonRequest;
import com.geer2.sop.sdk.response.CommonResponse;
import com.mz.mattress.api.enums.DeviceOptionEnum;
import com.mz.mattress.api.mattress.req.DeviceParamReq;
import com.mz.mattress.api.mattress.req.InspectionReq;
import com.mz.mattress.api.util.JacksonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class OpenClientApi {

//    private static final String url = "https://api.stage.geer2.com:9443/sop-gateway/";
//    private static final String appId = "202504091359643430048759808";
//    private static final String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDtpZMH+s0ISvZhF7QjWzePGxTkC9KnAE0u95mgiKJ1L8qeYF0GcsB0kt1JwUyuz8LGxbKJ/h+90hoKu3N6RkDDEINePXCU49p39i66px7HJ3olpt55pbGI29U7fEyP5Pe/Wt54p6ZLF1DfcR0/6N932bKkhkwicbXtpmsPEZWumqgHhMa/NaU2ubzK0h2iJVi06BhAVcxKvUUgXdXqMsX/fi0lOrDQqnnEC8nqwiYFlekY8oAZXaZGMnHOSAmwwSsidiC1pMw1fBiviNNczT4vze84agJRbI8JBK33r9Qu2pFl3wnysUY6wG42ESfAl1EeDghy1U2dUAPm4JJFWVgVAgMBAAECggEAA46n7cWmTgp5N6jYLHNQGJdKL8Oxiohpn1tbdFi3PdPST56XpeIP2W7dWx5GnJNFkh4hdwlTxk6fzytRJjJpA8J6hO2cgs4KViNcPbUEs+x2Wpvt1ppRVwAbaAGLT9gYkUe17s5rodIehsm1q2A8ABpFH0tXVminVYBZN+eGew+UM/NLprCy43Ryoq1x12DfOnBiT+Oywu5hwNeKMPcjF1PeUk8jfQ6Es+cNr7i0TZbt09+PYGhg5UshFsyceUM4jG6a2ip3LTvF4zLT7EcoBQqjHwxA1PuOaZZTEoyQq4AVYJAyXKbOZ6JHoneZF8gODU6AOecETFWTZChwgPrh3QKBgQD3DZuYCe6cEcfOYNQJb0E8CZEBB/YRwB3Gp5J7VfxzIbCVZQjuP5ubEMPjy3tu5+eTP9V83OL9LCuJBDrpuyoxgDbFdUt4nNMU2dr0uV5+1U7raR/2gFums+k0MXATyA3PBi43LYXe4NvunKRAWJU1Z52g1qcGfXkGBF6GxBQeGwKBgQD2QMLs80tSLCoDhNmrmciNlUKKOz3lLKQDPb3Ge9t+2Nr+QtB5tVRY+tjWhf3my2spA88ukE+F9TmyRITVW7RAcatRp93NzaXaFrtoZZCYKHtRqE8JJUEU6zoYux9V6d1+GEyiiFKIYMKA+Fp9noKLDCZqHUqrgG1gExcj7PIFjwKBgETHGAsCsNfoJ0zwLfeWbn3NV/4Gfl3As0lru170uDxBI8zjfNH/CU3pI+/V5T4h95lJyyBgyhybYF9bux74yS1QuW7NUb55QlZqtAcvV/xNXBQzhv90Co/lREYQgVgweI+q89DOrpc22GOcXim/v4JHn114hLVM1xVyv/IawlMDAoGAE/LJF6LZ3i0/tgtECtepXHbT7hjAedk07CJLRlHwRQVSMDi/ZKygyZiAilhOaizeYHMzjGUaXBKyetN8I0CLiJDsvO+pUyuunM0sn1di8ECPRN0qkeYXsJchnLFD55TLYZScWKpMbUnRV9tGkViO4K66SexXiS8dLSQ+wqVGMdcCgYEA2b8FFpN0Hw/H1YWP6K3+d4hHbf1PZDUsdIJrTq8yxDAU6Cmd90++wTj0RwtiyM0tSusqEYQjMouHlt1HYx62vD24J/cMtpgt2XclVhLHivsftFsJ05gWVEOzdX0AZ9wSFVuQAePRTvzm9j/MZ7EyA8flw2CohL1z8Jp3biizPKc=";

//    202505121371424286622875648

//    平台地址：https://openapi.geer2.com
//    账号：ype@third.com
//    密码：ype@123456

//    平台对外的接⼝调⽤地址
//    https://iot.geer2.com:9443/sop-gateway/

    //生产地址
    private static final String url = "https://iot.geer2.com:9443/sop-gateway/";
    private static final String appId = "202505121371424286622875648";
    private static final String privateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDOHqW2pufM9CyC\n" +
            "Kz5fyF9BoNVPCfMfcqHpAFU0+yETw5GF09pDMZnDAmfk4t/qlvsvSirL5mvl26ei\n" +
            "rPIAI/NmIDBz5gZ2UFruRupcK43XPFJeeZA4baM5ENbja/5M/Qi2nZ/DZJwiycru\n" +
            "UcS4xXpAXZC4PJP0ZwQ5F3HgbkeZocQgfEWLwvchmBcUqrtvm8yHzDD5DC5cqFw2\n" +
            "b+dX5SaxLE2W1PXjZWJ7YIUSZHaxiN2L+GfkZODS98le1edafpFhHXsfp22h3J7c\n" +
            "9+fIi9D5DCCkb1hgBOU8Y1WQ9gm4r7OVaEkt5Dm5iOam/tMqESQeq5px/c28NHEM\n" +
            "vwEbD8wPAgMBAAECggEAMvglJkuTmnzu3Klw3SKhW4s39DmToz0niISKjXVkMozz\n" +
            "2VIP8Pm8+hoIMvNfR5VaO9FtT8qE9+wfQfzeQuag86oWETLaejtYoP9sSKqEgqrp\n" +
            "+264GRwTdrMRUF+OHN+dXfsQXLLuYTPhOJmDueUrW+tHNCfWtmMbCjsf782VawnB\n" +
            "0QFjiZbV/y6eZX2hjrBOuhQJjHUEJgjnjsc3bbnTgAnusPhRpGV/ecdtgEnlydFe\n" +
            "aI2gsH0HEgzOFEku7LLeQwqF9gBpwemoYNKVuBqp1vp5Jid9mvIvD2VUXWNHEdvN\n" +
            "rVlnWqKyvRCFPmtQi6n28hyO1aUpNJghl1rLfUy2IQKBgQDvJNyDgk3J01rz/imJ\n" +
            "c/QfmhZHbZB+6bWzE/aXZIBUvpBdzhcej56eTN18R8vxMZF0NPoNiUKQpOdDBUsk\n" +
            "nBiMb+J1tth/lDxNfcr7ohqk7z3ch0PiMuwgbiP38SiLrYdePhvlboyhYikOHmXN\n" +
            "49QSe25KO7/14Dn+una1lmz3dwKBgQDcpeR/CziWcpfI4zJimZd6Nx8HZIUPGMOf\n" +
            "8i/q/tekZHLSg7nZx7y4waCV3BOLWV9xxsUWHN5Esc9w0UrD6iB3qKYC9+t2X3ZD\n" +
            "PfStjZaEIDtjGNTWNnOhfbXQAxMFl5t3aXKLDH31i4vp3Qmin/SNg3bsqIvFMQmr\n" +
            "7IvL4UOmKQKBgHR6vtFrOVpRkN6A8Fhb2w89bRaTXisCyJaV5ajtsbgVLZoTFJ21\n" +
            "FGxEQikTi1lKbG9uklDfh0WOnHm/bU9CnI5ivKnbSm4xlIGmAKU52sJ1KOtuEvlt\n" +
            "FifhnbFZjAckmWek1s+UXfkJJ7gRxEJZcuUhq6GKosAu2/Cff8yInUCXAoGACQvo\n" +
            "LWlJXam/HWZrR22ir0kZRZVSe0b5plIossrWDpBrwMPT1+D/lNejfOF0D6+PYTvU\n" +
            "Es8GMezUY3zBQv0TbbSFcfO2PLk3QRSGNqNqe2pS5QI8n7fFz+Y2kRu/KOiJYED7\n" +
            "iEXv2CRrjSAZ6AgE39R3CMb0TBZxnhHpvWcz/dkCgYAbAtDC5/xtCHBfHQUaCAVY\n" +
            "UDiegNINHgy/4N1xqOHfgpOBpIyde0qe0qTvDQbsRzPKXi40mouygNz54K4rbqiN\n" +
            "pLJMcEJYm+ImsJAsRK6ypLonSadlrq/PzJNVTQxelFXo9skDN44nGB9rmXCvv7zv\n" +
            "qi7boWkpzPlsdbB3j8xOJQ==";

    private static OpenClient client = new OpenClient(url, appId, privateKey);

    public void sendOptions(String deviceCode, String productKey, DeviceOptionEnum optionEnum, Integer value) {

        if (value == null) {
            if (optionEnum == DeviceOptionEnum.start) {
                value = 1;
            } else if (optionEnum == DeviceOptionEnum.pause) {
                value = 0;
            } else if (optionEnum == DeviceOptionEnum.stop) {
                value = 4;
            } else {
                return;
            }
        }

        DeviceParamReq req = new DeviceParamReq();
        req.setIdentifier(optionEnum.getCode());
        req.setDataValue(value);

        List<DeviceParamReq> list = new ArrayList<>();
        list.add(req);

        exec(deviceCode, productKey, null, list);
    }

    public void sendInspection(InspectionReq req) {

        List<DeviceParamReq> list = new ArrayList<>();
        //性别 1男 2女
        DeviceParamReq req1 = new DeviceParamReq("111c", req.getSex());
        list.add(req1);
        //年龄
        DeviceParamReq req2 = new DeviceParamReq("111d", req.getAge());
        list.add(req2);
        //体重
        DeviceParamReq req3 = new DeviceParamReq("111e", req.getWeight());
        list.add(req3);
        //问答一
//        问答一：111F 0001
//        面舌情况
//        1、面色萎黄或淡白、舌淡胖嫩、舌边有齿痕
//        2、面色苍白或灰暗、毛发易落、羞明畏光、口唇色淡
//        3、面色潮红、两目干涩、皮肤干燥、舌红少津
//        4、面色黄胖、油脂较多、舌体胖大、舌苔白
//        5、面垢油光、易生痤疮、眼睑红赤、舌质红、苔黄腻
//        6、神情忧郁、面色青暗
//        7、面色晦暗，容易出现淤斑、口唇暗紫、舌下有淤点
//        8、无一定特征，或者先天畸形、生理缺陷
//        9、以上问题均无
        DeviceParamReq req4 = new DeviceParamReq("111f", req.getQuest1());
        list.add(req4);
        //问答二
//        问答二：1120 0001
//        排汗及大小便情况
//        1、易出汗、小便清长、夜尿频多、便秘或大便不成形
//        2、易出汗
//        3、大便干燥
//        4、多汗且黏黄、小便混浊
//        5、大便黏滞或燥结，小便赤短
//        6、排便紧张且大便稀烂
//        7、大便色黑或柏油状，或有便血状况
//        8、易呕吐、腹泻
//        9、以上问题均无
        DeviceParamReq req5 = new DeviceParamReq("1120", req.getQuest2());
        list.add(req5);
        //问答三
//        问答三：1121 0001
//        常见症状
//        1、头晕、自汗、乏力、食欲减退、心悸气短
//        2、平时畏冷、易胃腹痛、消化不良、水肿
//        3、手足心热、口燥咽干、视物模糊、眩晕耳鸣
//        4、腹胀、痰多、眩晕、胸闷、食欲不振
//        5、口干口苦，易生痤疮粉刺
//        6、性格内向、不喜社交、敏感多虑
//        7、易疼痛，刺痛，肿块，肤有色斑
//        8、易打喷嚏，红疹，瘙痒、哮喘、皮肤脱屑
//        9、以上问题均无
        DeviceParamReq req6 = new DeviceParamReq("1121", req.getQuest3());
        list.add(req6);

        exec(req.getDeviceCode(), req.getProductKey(), req.getMemberId(), list);
    }

    private void exec(String deviceCode, String productKey, Long memberId, List<DeviceParamReq> list) {

        // 创建请求对象
        CommonRequest request = new CommonRequest("device.common.control");

        Map<String, Object> bizModel = new HashMap<>();
        bizModel.put("productKey", productKey);
        bizModel.put("deviceCode", deviceCode);

        if (memberId != null) {
            //是检测报告
            bizModel.put("customerId", memberId);
            bizModel.put("customerIdInsert", 1);
        }

        bizModel.put("inParameters", list);
        request.setBizModel(bizModel);

        String json = JacksonUtils.toJson(list);

        log.info("open_client_req=" + json);

        // 发送请求
        CommonResponse response = client.execute(request);

        String body = response.getBody();

        log.info("open_client_resp=" + body);
    }

    public void queryStatus(String deviceCode, String productKey) {

        // 创建请求对象
        CommonRequest request = new CommonRequest("device.common.control");

        Map<String, Object> bizModel = new HashMap<>();
        bizModel.put("productKey", productKey);
        bizModel.put("deviceCode", deviceCode);
        bizModel.put("method", "search");
        request.setBizModel(bizModel);

        log.info("open_client_req_search=" + deviceCode);

        // 发送请求
        CommonResponse response = client.execute(request);

        if (response != null) {
            String body = response.getBody();
            log.info("open_client_resp_search=" + body);
        }

    }

    public static void main(String[] args) {

        System.out.println(privateKey);

    }


}
